package com.lxl.fcexcel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.lxl.fcexcel.commons.PropertiesConstant;
import com.lxl.fcexcel.pojo.Goods;
import com.lxl.fcexcel.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @author 北上寻海
 *
 * Scope("prototype")
 */
@Component
@Scope("prototype")
public class WebGoodsListener extends AnalysisEventListener<Goods> {

    @Autowired
    GoodsService goodsService;

    ArrayList<Goods> list = new ArrayList<Goods>();

    @Override
    public void invoke(Goods  goods , AnalysisContext analysisContext) {

        list.add(goods);
        //每读入五条数据保存一次
        if(list.size() % PropertiesConstant.NUMBER_OF_MESSAGES_SAVE == 0){
            goodsService.saveBatch(list);
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
